package com.heima.a07_demo.server.service.impl;

import com.heima.a07_demo.common.exception.CommonException;
import com.heima.a07_demo.pojo.dto.DoctorDTO;
import com.heima.a07_demo.pojo.dto.DoctorImageDTO;
import com.heima.a07_demo.pojo.dto.DoctorPasswordDTO;
import com.heima.a07_demo.pojo.dto.DoctorRegisterDTO;
import com.heima.a07_demo.pojo.entity.Doctor;
import com.heima.a07_demo.server.mapper.DoctorMapper;
import com.heima.a07_demo.server.service.DoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
@Slf4j
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /*
    *
    * 新增医生
    * */
    @Override
    public void save(DoctorRegisterDTO doctorRegisterDTO) {
        //获取验证码
        String code=doctorRegisterDTO.getCode();
        String phoneNumber= doctorRegisterDTO.getPhoneNumber();;
        //跟发送的验证码进行比较
        String key="A07:login:code:"+phoneNumber;
        String cacheCode=stringRedisTemplate.opsForValue().get(key);
        if(cacheCode==null||!code.equals(cacheCode)){
            System.out.println("验证码错误");
            throw new CommonException("验证码错误");

        }
       Doctor doctor=new Doctor();
        //对象属性拷贝,属性名必须一致
        //BeanUtils.copyProperties(doctorRegisterDTO,doctor);
        doctor.setDoctorTitle(doctorRegisterDTO.getDoctorTitle());
        doctor.setName(doctorRegisterDTO.getName());
        doctor.setUsername(doctorRegisterDTO.getUsername());
        doctor.setEmail(doctorRegisterDTO.getEmail());
        doctor.setPhoneNumber(doctorRegisterDTO.getPhoneNumber());
        doctor.setDepartment(doctorRegisterDTO.getDepartment());
        doctor.setPassword(DigestUtils.md5DigestAsHex(doctorRegisterDTO.getPassword().getBytes()));
        log.info("测试拷贝是否有问题:{}",doctor);
        doctorMapper.insert(doctor);

    }
/*
*
* 根据id查询医生信息
*
* */
    @Override
    public Doctor getById(Long id) {
        Doctor doctor=doctorMapper.getById(id);
        return doctor;
    }

    /*
    * 修改医生信息
    * */

    @Override
    public void update(DoctorDTO doctorDTO) {
        Doctor doctor=new Doctor();
        //拷贝对象属性
        BeanUtils.copyProperties(doctorDTO,doctor);
       log.info("测试拷贝是否有问题:{}",doctor);
        log.info("id为:{}",doctor.getDoctorId());
      /*  doctor=Doctor.builder()
                .doctorId(doctorDTO.getDoctorId())
                .gender(doctorDTO.getGender())
                .age(doctorDTO.getAge())
                .username(doctorDTO.getUsername())
                .name(doctorDTO.getName())
                .phoneNumber(doctorDTO.getPhoneNumber())
                .build();*/
        doctorMapper.update(doctor);
    }

    /*
    * 根据id查头像
    * */
    @Override
    public String getImageById(Long id) {
        String filepath=doctorMapper.getImageById(id);
        return filepath;
    }

    /*
     * 修改头像
     * */

    @Override
    public void updateImage(DoctorImageDTO doctorImageDTO) {
      String filepath=doctorImageDTO.getImage();
      Long id=doctorImageDTO.getDoctorId();
      doctorMapper.updateImage(filepath,id);
    }

    /*
    * 修改密码
    * */
    @Override
    public void updatePassword(DoctorPasswordDTO doctorPasswordDTO) {
        //根据id查出信息
        Long id=doctorPasswordDTO.getDoctorId();
        Doctor doctor=new Doctor();
        doctor=doctorMapper.getById(id);
        String password=doctor.getPassword();
        //判断旧密码是否输对
        //给旧密码加密
        String oldPassword=doctorPasswordDTO.getOldPassword();
        oldPassword= DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if(!password.equals(oldPassword)){
            System.out.println("旧密码输入错误");
            throw new CommonException("旧密码输入错误");
        }
        //修改为新密码
        String newPassword=doctorPasswordDTO.getNewPassword();
        newPassword=DigestUtils.md5DigestAsHex(newPassword.getBytes());
        //保存新密码
        doctorMapper.savePassword(newPassword,id);

    }


}
